package com.pic;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.util.Utils;
import com.example.com.skills_utf8.R;


//
public class _61Fragment extends Fragment {



    LinearLayout layout_a, layout_b, bar_container;
    TextView tv_a;
    TextView tv_b;



    int last_index = -1;
    boolean bChange = false;




    public _61Fragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_61, container, false);


        initBar(view);





        return view;

    }



    ////////////////////////////////////////////////////////////////////////////////////



    ImageView iv_flag ;

    void initBar(View view){

        iv_flag = (ImageView)view.findViewById(R.id.iv_flag);

        bar_container = (LinearLayout) view.findViewById(R.id.layout_bar_container);
        layout_a = (LinearLayout) view.findViewById(R.id.layout_a);
        layout_b = (LinearLayout) view.findViewById(R.id.layout_b);
        tv_a = (TextView) view.findViewById(R.id.tv_a);
        tv_b = (TextView) view.findViewById(R.id.tv_b);

        ViewTreeObserver ob = tv_a.getViewTreeObserver();
        ob.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                // TODO Auto-generated method stub

                if (last_index == -1) {
                    ShowColor(0);
                }

                return true;
            }
        });

        layout_a.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (Utils.isFastDoubleClick())
                    return;

                ShowColor(0);

            }
        });
        layout_b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(Utils.isFastDoubleClick())
                    return;

                ShowColor(1);

            }
        });


    }



    void ShowColor(int index) { //

        if (last_index == -1) {  // 初始化到第一个位置
            // 初始化
            ImageView bottom_triangle = iv_flag;

            float fromXDelta = 0;
            float toXDelta = layout_a.getLeft();
            int width = layout_a.getWidth();
            int child_width = bottom_triangle.getWidth();
            toXDelta += width / 2 - child_width / 2; //

            TranslateAnimation ta = new TranslateAnimation(fromXDelta,
                    toXDelta, 0, 0);
            ta.setFillAfter(true);

            bottom_triangle.startAnimation(ta);
            //

            last_index = index;
            return;
        }

        View last_view = bar_container.getChildAt(last_index);
        View now = bar_container.getChildAt(index);
        last_index = index;

        switch (index) {
            case 0:
                tv_a.setTextColor(getResources().getColor(R.color.white));
                tv_b.setTextColor(getResources().getColor(R.color.solid_red));

                trans(last_view, layout_a); // tv_a
                break;

            case 1:
                tv_a.setTextColor(getResources().getColor(R.color.solid_red));
                tv_b.setTextColor(getResources().getColor(R.color.white));

                trans(last_view, layout_b); // tv_b
                break;

        }

    }

    void trans(View last, View now) {

        ImageView bottom_triangle = iv_flag;

        float fromXDelta = last.getLeft();
        float toXDelta = now.getLeft();

        int width = now.getWidth();
        int child_width = bottom_triangle.getWidth();
        toXDelta += width / 2 - child_width / 2; //
        fromXDelta += width / 2 - child_width / 2;

        TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
        ta.setFillAfter(true);
        ta.setDuration(500);
        bottom_triangle.startAnimation(ta);
    }



//////////////////////////////////







}
